const Koa = require("koa");
const route = require("koa-route");
const app = new Koa();
const { generatePrimes } = require("./block_primes");

const main = (ctx) => {
  ctx.response.body = "Hello World!";
};

const runGeneratePrimes = (min, max) => {
  console.log("---->runGeneratePrimes start");
  const startTime = new Date().getTime();
  generatePrimes(min, max);
  const endTime = new Date().getTime();
  let costTime = endTime - startTime;
  console.log("---->runGeneratePrimes end, costTime:", costTime);
  return costTime;
};

const test = (ctx) => {
  const min = 2;
  const max = 1e7;
  let cost = runGeneratePrimes(min, max);
  ctx.response.body = `计算素数完成,耗时${cost}`;
};

app.use(route.get("/test", test));
app.use(route.get("/", main));
console.log("start listen 3000");
app.listen(3000);
